package com.metronoteapp;

import java.util.ArrayList;
import java.util.List;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
import com.metronoteapp.adapter.ExpandGroupNoteAdapter;
import com.metronoteapp.database.GroupNoteDao;
import com.metronoteapp.database.NoteDao;
import com.metronoteapp.model.GroupModel;
import com.metronoteapp.model.NoteModel;

public class SettingGroupExpActivity extends ExpandableListActivity implements
		OnChildClickListener {

	private String TAG = SettingGroupExpActivity.class.getSimpleName();
	private GroupModel mObjGroupModel;
	private NoteModel mObjNoteModel;
	private List<NoteModel> mListObjNote;
	private ArrayList<Object> groupItem = new ArrayList<Object>();
	private ArrayList<Object> childItem = new ArrayList<Object>();
	private NoteDao mObjNoteDao;
	private GroupNoteDao mObjGroupDao;
	private Context mContext;
	private ExpandableListView expandbleLis;
	private ExpandGroupNoteAdapter mNewAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		expandbleLis = getExpandableListView();
		expandbleLis.setDividerHeight(2);
		expandbleLis.setGroupIndicator(null);
		expandbleLis.setClickable(true);

		// Setting data for group list
		setGroupData();

		// Setting data for Child list
		setChildGroupData();
		mNewAdapter = new ExpandGroupNoteAdapter(mContext, groupItem, childItem);
		mNewAdapter
				.setInflater(
						(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
						this);
		getExpandableListView().setAdapter(mNewAdapter);
		expandbleLis.setOnChildClickListener(this);

		// Setting : only show one group on same time
		expandbleLis.setOnGroupExpandListener(new OnGroupExpandListener() {

			public void onGroupExpand(int groupPosition) {
				int len = mNewAdapter.getGroupCount();

				for (int i = 0; i < len; i++) {
					if (i != groupPosition) {
						expandbleLis.collapseGroup(i);
					}
				}
			}
		});
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		Toast.makeText(SettingGroupExpActivity.this, "Clicked On Child",
				Toast.LENGTH_SHORT).show();
		return true;
	}

	/*
	 * This function parse cursor
	 */
	private static String getColumnAsString(Cursor cursor, int index) {
		return cursor.getString(index).trim();
	}

	/*
	 * Setting data group
	 */
	public void setGroupData() {
		mObjGroupDao = new GroupNoteDao(mContext);
		mObjGroupDao.openDB();
		Cursor mCursor = mObjGroupDao.getAllRecord();
		if (mCursor.moveToFirst()) {
			do {
				mObjGroupModel = new GroupModel();
				mObjGroupModel.setGroupId(Integer.parseInt(getColumnAsString(
						mCursor, 0)));
				mObjGroupModel.setGroupName(getColumnAsString(mCursor, 1));
				mObjGroupModel.setColor(getColumnAsString(mCursor, 2));
				groupItem.add(mObjGroupModel);
			} while (mCursor.moveToNext());
		}
		mObjGroupDao.closeDB();
	}

	/*
	 * Setting data child
	 */
	public void setChildGroupData() {
		mObjNoteDao = new NoteDao(mContext);
		mObjNoteDao.openDB();
		for (int i = 0; i < groupItem.size(); i++) {
			mObjGroupModel = new GroupModel();
			mObjGroupModel = (GroupModel) groupItem.get(i);
			mListObjNote = new ArrayList<NoteModel>();
			mObjNoteModel = new NoteModel();
			mObjNoteModel.setGroupId(mObjGroupModel.getGroupId());
			Log.v(TAG, "setChildGroupData() | Setting items of group id: "
					+ mObjGroupModel.getGroupId());
			Cursor mCursor = mObjNoteDao.getNoteByGroup(mObjNoteModel);
			if (mCursor.moveToFirst()) {
				do {
					Log.v(TAG,
							"setChildGroupData() | Found items: "
									+ mCursor.getCount());
					mObjNoteModel = new NoteModel();
					mObjNoteModel.setId(Integer.parseInt(getColumnAsString(
							mCursor, 0)));
					mObjNoteModel.setTitle(getColumnAsString(mCursor, 1));
					mObjNoteModel.setContent(getColumnAsString(mCursor, 2));
					mObjNoteModel.setNoteType(getColumnAsString(mCursor, 3));
					mObjNoteModel.setDateTime(getColumnAsString(mCursor, 4));
					mObjNoteModel.setGroupId(Integer
							.parseInt(getColumnAsString(mCursor, 5)));
					mObjNoteModel.setFlgFav(Integer.parseInt(getColumnAsString(
							mCursor, 6)));
					mListObjNote.add(mObjNoteModel);
				} while (mCursor.moveToNext());
			}
			childItem.add(mListObjNote);
		}
		mObjNoteDao.closeDB();
	}
}
